package com.gouwudai.gobbs.listener;

import java.io.File;
import java.util.LinkedHashMap;
import java.util.List;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.gouwudai.gobbs.common.Arguments;
import com.gouwudai.gobbs.pojo.TbbsDictParam;
import com.gouwudai.gobbs.service.IClassService;
import com.gouwudai.gobbs.service.IDictParamService;
import com.litt.core.cache.OSCacheManager;
import com.litt.core.common.BeanManager;
import com.litt.core.common.Utility;
import com.litt.core.common.XMLConfig;
import com.litt.core.exception.BusiException;

/**
 *  
 * <b>标题:</b> 系统初始化任务.
 * <pre><b>描述:</b> 
 *   配置文件读取、数据缓存等
 * </pre>
 * 
 * @author <a href="mailto:littcai@hotmail.com">空心大白菜</a>
 * @since 2007-3-29
 * @version 1.0
 *
 */
public class InitSystemListener implements ServletContextListener {

    private static final Log logger = LogFactory.getLog(InitSystemListener.class);

	/**
	 * 
	 * 执行容器销毁相关任务
	 * @param contextEvent
	 */
	public void contextDestroyed(ServletContextEvent contextEvent) {
		// TODO Auto-generated method stub		
	}


	/**
	 * 
	 * 执行容器初始化相关任务
	 * @param contextEvent
	 */
	public void contextInitialized(ServletContextEvent contextEvent){
		//初始化系统配置文件
		logger.info("系统初始化...");	

		logger.info("读取系统配置文件 - gobbs.cfg.xml");
        try
        {
            XMLConfig.init(Utility.getClassLoaderPath()+"gobbs.cfg.xml");
        }
        catch(BusiException e)
        {
            logger.error("读取系统配置文件失败 - gobbs.cfg.xml",e);
            throw new RuntimeException("读取系统配置文件失败 - gobbs.cfg.xml",e);            
        }        
		//初始化全局对象（全局对象统一放在Arguments里）
		Arguments.runningPath = contextEvent.getServletContext().getRealPath("/");	        
		logger.info("系统运行路径 - "+Arguments.runningPath);
        String contentPath = Arguments.getRootPath(); //WebContent的相对路径
        String temp[] = Utility.spitString(contentPath, "\\"+File.separator);
        contentPath = temp[temp.length-1];        
        logger.info("WebContent路径 - "+contentPath);
        Arguments.contentPath = contentPath;
		Arguments.userHeadsPath = Arguments.getRootPath() + XMLConfig.getValue("user-heads-path");
		//缓存参数字典表信息
		logger.info("缓存参数字典表信息...");	
		IDictParamService dictParamService = (IDictParamService)BeanManager.getBean("dictParamService");
		try
		{
			List list = dictParamService.listAll();
			LinkedHashMap<String,String> dictCache = new LinkedHashMap<String,String>(list.size());
			TbbsDictParam pojo = null;
			for(int i=0;i<list.size();i++)
			{
				pojo = (TbbsDictParam)list.get(i); 				
				dictCache.put(pojo.getDictType()+"."+pojo.getDictValue(), pojo.getDictContent());
			}	
			OSCacheManager.storeCacheObject("DICT_PARAM", dictCache);
		}
		catch(Exception e)
		{
			logger.error("缓存参数字典表信息失败！",e);
            throw new RuntimeException("缓存参数字典表信息失败！",e);
			
		}
		//缓存分类/版面下拉列表
		logger.info("缓存分类/版面下拉列表...");	
		IClassService classService = (IClassService)BeanManager.getBean("classService");
		try
		{
		    //后台下拉列表，包含隐藏的分类或版面
			OSCacheManager.storeCacheObject("CLASS_OPTIONS", classService.listClassByType(0,null));
		    OSCacheManager.storeCacheObject("ALL_CLASS_OPTIONS", classService.listClassByType(null,null));	    	
		}
		catch(Exception e)
		{
			logger.error("缓存分类/版面下拉列表失败！",e);
		}
	}
}
